package com.example.schoolexam2.mapper;

import com.example.schoolexam2.entity.QuestionCheckd;
import com.example.schoolexam2.entity.QuestionFill;
import com.example.schoolexam2.entity.QuestionJudge;
import com.example.schoolexam2.entity.QuestionRadio;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface QuestionMapper {
    /**
     * 通过id查找试题
     * @param id
     * @return
     */
    @Select("select * from question_fill where questionId=#{id}")
    QuestionFill findFillById(String id);
    @Select("select * from question_checkd where questionId=#{id}")
    QuestionCheckd findCheckdById(String id);
    @Select("select * from question_judge where questionId=#{id}")
    QuestionJudge findJudgeById(String id);
    @Select("select * from question_radio where questionId=#{id}")
    QuestionRadio findRadioById(String id);

    /**
     * 将题目实体保存到数据库中
     * @param radio
     */
    @Insert("insert into question_radio values(#{questionId},#{questionName},#{questionA},#{questionB},#{questionC},#{questionD},#{questionAnswer},#{questionDiscribe},#{questionType},0,1,#{teacherId})")
    void saveRadio(QuestionRadio radio);
    @Insert("insert into question_judge values(#{questionId},#{questionName},#{questionAnswer},#{questionDiscribe},#{questionType},0,1,#{teacherId})")
    void saveJudge(QuestionJudge judge);
    @Insert("insert into question_checkd values(#{questionId},#{questionName},#{questionA},#{questionB},#{questionC},#{questionD},#{questionAnswer},#{questionDiscribe},#{questionType},0,1,#{teacherId})")
    void saveCheck(QuestionCheckd checkd);
    @Insert("insert into question_fill values(#{questionId},#{questionName},#{questionAnswer},#{questionDiscribe},#{questionType},0,1,#{teacherId})")
    void saveFill(QuestionFill fill);


    /**
     * 查询试题的id和名称
     * @return
     */
    List<QuestionRadio> findAllRadio(@Param("department")String[] department, @Param("isPublic")String isPublic, @Param("start")int start, @Param("size")int end,@Param("qname")String qname);
    List<QuestionCheckd> findAllCheckd(@Param("department")String[] department, @Param("isPublic")String isPublic,@Param("start")int start,@Param("size")int end,@Param("qname")String qname);
    List<QuestionFill> findAllFill(@Param("department")String[] department, @Param("isPublic")String isPublic,@Param("start")int start,@Param("size")int end,@Param("qname")String qname);
    List<QuestionJudge> findAllJudge(@Param("department")String[] department, @Param("isPublic")String isPublic,@Param("start")int start,@Param("size")int end,@Param("qname")String qname);


    /**
     * 保存学生的选择  通过学生id删除上次保 存的选择 通过学生的id和考试id查询学生当前试卷的选择
     */
    void saveStudentChoose(String studentId,String examId,String questionId,String questionType,String studentChoose);
    @Delete("delete from student_question_exam where studentId=#{studentId}")
    void delStudentOldChoose(String studentid);
    String getStudentChoose(String studentId,String exmaId,String questionType,String questionId);
    /**
     * 查询试题的所有值
     * @return
     */
    @Select("select * from question_radio")
    List<QuestionRadio> findAllRadio1();
    @Select("select * from question_checkd")
    List<QuestionCheckd> findAllCheckd1();
    @Select("select * from question_fill")
    List<QuestionFill> findAllFill1();
    @Select("select * from question_judge")
    List<QuestionJudge> findAllJudge1();

    /**
     * 如果试卷被选中，就将其ischoose属性改为1
     */
    @Update("update question_radio set isChoose=1 where questionId=#{questionId}")
    void changeChooseRadio(String questionId);
    @Update("update question_checkd set isChoose=1 where questionId=#{questionId}")
    void changeChooseCheckd(String questionId);
    @Update("update question_fill set isChoose=1 where questionId=#{questionId}")
    void changeChooseFill(String questionId);
    @Update("update question_judge set isChoose=1 where questionId=#{questionId}")
    void changeChooseJudge(String questionId);

    /**
     * 根据试题id删除学生的,如果删除成功，返回值就大于0
     */
    @Update("delete from question_radio where questionId=#{questionId} and isChoose=0")
    int delQuestionRadio(String questionId);
    @Update("delete from question_radio where questionId=#{questionId} and isChoose=0")
    int delQuestionCheckd(String questionId);
    @Update("delete from question_radio where questionId=#{questionId} and isChoose=0")
    int delQuestionFill(String questionId);
    @Update("delete from question_radio where questionId=#{questionId} and isChoose=0")
    int delQuestionJudge(String questionId);

    /**
     * 保存用户的修改
     */
    @Update("update question_radio set questionName=#{questionName},questionA=#{questionA},questionB=#{questionB},questionC=#{questionC},questionD=#{questionD},questionAnswer=#{questionAnswer},questionDiscribe=#{questionDiscribe} where questionId=#{questionId}")
    int updateRadio(QuestionRadio radio);
    @Update("update question_checked set questionName=#{questionName},questionA=#{questionA},questionB=#{questionB},questionC=#{questionC},questionD=#{questionD},questionAnswer=#{questionAnswer},questionDiscribe=#{questionDiscribe} where questionId=#{questionId}")
    int updateCheckd(QuestionCheckd checkd);
    @Update("update question_fill set questionName=#{questionName},questionAnswer=#{questionAnswer},questionDiscribe=#{questionDiscribe} where questionId=#{questionId}")
    int updateFill(QuestionFill fill);
    @Update("update question_judge set questionName=#{questionName},questionAnswer=#{questionAnswer},questionDiscribe=#{questionDiscribe} where questionId=#{questionId}")
    int updateJudge(QuestionJudge judge);
    /**
     * 将选择的isShow转换成0
     */
    @Update("update question_radio set isShow=0 where questionId=#{questionId}")
    int changeShowRadio(String questionId);
    @Update("update question_checked set isShow=0 where questionId=#{questionId}")
    int changeShowCheckd(String questionId);
    @Update("update question_fill set isShow=0 where questionId=#{questionId}")
    int changeShowFill(String questionId);
    @Update("update question_judge set isShow=0 where questionId=#{questionId}")
    int changeShowJudge(String questionId);

    /**
     *查询表中有多少记录
     */

    int findRadioCount(@Param("department")String[] department, @Param("isPublic")String isPublic,@Param("qname")String qname);

//    @Select("select count(*) from question_checkd")
    int findCheckdCount(@Param("department")String[] department, @Param("isPublic")String isPublic,@Param("qname")String qname);

//    @Select("select count(*) from question_fill")
    int findFillCount(@Param("department")String[] department, @Param("isPublic")String isPublic,@Param("qname")String qname);

//    @Select("select count(*) from question_judge")
    int findJudgeCount(@Param("department")String[] department, @Param("isPublic")String isPublic,@Param("qname")String qname);
}
